<?php /* УТФ-8:fix */

if( class_exists('SPI') === false ) // check class for autoload it
{
    die('SPIDER is dead :(');
}

function __autoload_requre( $source )
{
    if( file_exists( $source ) === false )
    {
        return false;
    }

    require( $source );
    return true;
}

function __autoload( $classname )
{
    $FOLDER_SPIDER_CORE = SPIcfg::$dir['core'];

    switch( $classname )
    {
        case 'SPI':
            return __autoload_requre( $FOLDER_SPIDER_CORE.'core.php' );

        case 'SPIstore':
            return __autoload_requre( $FOLDER_SPIDER_CORE.'store.php' );

        case 'SPItpl':
            return __autoload_requre( $FOLDER_SPIDER_CORE.'tpl.php' );

        case 'SPIm':
            return __autoload_requre( $FOLDER_SPIDER_CORE.'models.php' );

        case 'SPIv':
            return __autoload_requre( $FOLDER_SPIDER_CORE.'views.php' );

        case 'SPIc':
            return __autoload_requre( $FOLDER_SPIDER_CORE.'controllers.php' );
    }

    if( preg_match( '/^(SPI[a-z]+)__([A-Za-z0-9_]+)$/u', $classname, $matches ) > 0 )
    {
        $group_id   = $matches[1];
        $fid        = str_replace( '__', '.', $matches[2] );
        $base_fid   = array_shift( explode( '.', $fid ) );

        switch( $group_id )
        {
            case 'SPIcore':
                return __autoload_requre( $FOLDER_SPIDER_CORE."core.{$fid}.php" );

            case 'SPIstore':
                return __autoload_requre( $FOLDER_SPIDER_CORE."store.{$fid}.php" );

            case 'SPItpl':
                return __autoload_requre( $FOLDER_SPIDER_CORE."tpl.{$fid}.php" );

            case 'SPImodel':
                return __autoload_requre( $FOLDER_SPIDER_CORE."modules/{$base_fid}/m.{$fid}.php" );

            case 'SPIview':
                return __autoload_requre( $FOLDER_SPIDER_CORE."modules/{$base_fid}/v.{$fid}.php" );

            case 'SPIcontroller':
                return __autoload_requre( $FOLDER_SPIDER_CORE."modules/{$base_fid}/c.{$fid}.php" );

            case 'SPIlang':
            {
                list( $lang_id ) = explode( '.', $fid );
                return __autoload_requre( "langauges/{$lang_id}/lang.{$fid}.php" );
            }
        }
    }
	
	return __autoload_requre( $FOLDER_SPIDER_CORE."components/{$classname}.php" );
}
